﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Runtime.Serialization;
using MDCAdminTool.MDC_API;

namespace MDCAdminTool
{
    public class FindCluster
    {
        public string Name              { get; set; }
        public string Provider          { get; set; }
        public string ConnectionString  { get; set; }
        public string ConfigFile        { get; set; }
        public string ConfigPath        { get; set; }
        public bool IsSqlConfig         { get; set; }

        private const string ConfigFileName  = @"DistributedCache.exe.config";
        private const string DefaultPath     = @"C:\Program Files\Microsoft Distributed Cache\V1.0";
        private DataCacheConfigSection dataCacheConfig;


        

        private bool Find()
        {
            try
            {
                var file = Path.Combine( DefaultPath , ConfigFileName );
                if ( File.Exists( file ) )
                {
                    InitDataCacheConfigSection(file);
                }
                else
                {
                    if( string.IsNullOrEmpty( ConfigPath ) )
                        return false;
                    else
                    {
                        InitDataCacheConfigSection( Path.Combine( ConfigPath , ConfigFileName ) );
                    }
                }
            }
            catch ( Exception exception)
            {
                return false;
            }
            return true;
        }
        
        private void InitDataCacheConfigSection( string file )
        {
            var config  = XElement.Load( file );
            XElement dcc = config.Descendants( "dataCacheConfig" ).First();
            dataCacheConfig = new DataCacheConfigSection
                                  {
                                      ClusterName   = dcc.Attribute( "clusterName" ).Value ,
                                      HostName      = dcc.Attribute( "hostName" ).Value ,
                                      CacheHostName = dcc.Attribute( "cacheHostName" ).Value ,
                                      Provider      = dcc.Element( "clusterConfig" ).Attribute( "provider" ).Value ,
                                      ConnectionString = dcc.Element( "clusterConfig" ).Attribute( "connectionString" ).Value
                                  };
        }
        
        public bool InitCluster( out CacheCluster cluster )
        {
            if( Find() == true && dataCacheConfig != null )
            {
                cluster = new CacheCluster( 
                                    dataCacheConfig.ClusterName , 
                                    dataCacheConfig.Provider    , 
                                    dataCacheConfig.ConnectionString );

                var db = new DBConfigDataContext(dataCacheConfig.ConnectionString);
                int? port = (from x in db.NodeConfigs
                            where x.Host == dataCacheConfig.HostName.ToLower()
                            select x).First().Port; 

                //cluster.Hosts.Add( new CacheHost( dataCacheConfig.HostName , port ?? 0 ) );
            }
            else
            {
                cluster = null;
                return false;
            }
            return true;
        }
    }
}
